package util
{
	import flash.display.BitmapData;
	import flash.display.CapsStyle;
	import flash.display.InterpolationMethod;
	import flash.display.JointStyle;
	import flash.display.LineScaleMode;
	import flash.display.Shape;
	import flash.display.SpreadMethod;
	import flash.geom.Matrix;
	import flash.text.TextFormat;
	
	import enums.ColorLib;
	
	import feathers.controls.Button;
	import feathers.controls.Radio;
	import feathers.controls.TextInput;
	import feathers.controls.renderers.DefaultListItemRenderer;
	import feathers.display.Scale3Image;
	import feathers.textures.Scale3Textures;
	
	import starling.display.Image;
	import starling.textures.Texture;
	import starling.utils.Color;
	
	/**
	 * FlashGraphic to FeathersUI 生成器
	 * @author £iang
	 * 
	 */	
	public class FG2FUGenerator
	{
		//-----------------------------
		// variables
		//-----------------------------
		
		/** 小字体 */
		public static const TEXT_SIZE_SMALL:String = "small";
		/** 大字体 */
		public static const TEXT_SIZE_LARGE:String = "large";
		
		//-----------------------------
		// properties
		//-----------------------------
		
		
		//-----------------------------
		// setter&getter
		//-----------------------------
		
		
		//-----------------------------
		// methods
		//-----------------------------
		
		public static function generateRoundRectButton( 
			width:Number, height:Number, 
			ratio:Number, raduis:Number,
			textSize:String, isItem:Boolean = false
		):Button
		{
			var fButton:Button = new Button();
		
			fButton.width = width  * ratio;
			fButton.height = height * ratio;
			
			
			fButton.defaultSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "default"
			);
			fButton.downSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "down"
			);
			fButton.disabledSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "disabled"
			);
			fButton.defaultSelectedSkin = drawRoundRectImage2(
				height, ColorLib.ORANGE, ratio, raduis, "default"
			);
			fButton.disabledSkin.alpha = 0.4;
			
			var fontNames:String = "微软雅黑,宋体";
			var textFormatDefault:TextFormat;
			
			
			if ( isItem )
			{
				switch( textSize )
				{
					case "small":
					{
						textFormatDefault = new TextFormat( fontNames, 20 * ratio, Color.WHITE, false, 
							null, null, null, null, "center", null, null, null, -5 * ( 1.5 - ratio ) );
						break;
					}
						
					case "large":
					{
						textFormatDefault = new TextFormat( fontNames, 24 * ratio, Color.WHITE, false, 
							null, null, null, null, "center", null, null, null, -5 * ( 1.5 - ratio ) );
						break;
					}
						
					default:
					{
						break;
					}
				}
				
				fButton.defaultLabelProperties.textFormat = 
					textFormatDefault;
			}
			else
			{
				switch( textSize )
				{	
					case "small":
					{
						textFormatDefault = new TextFormat( fontNames, 24 * ratio, Color.WHITE, true );
						break;
					}
						
					case "large":
					{
						textFormatDefault = new TextFormat( fontNames, 36 * ratio, Color.WHITE, true );
						break;
					}
						
					default:
					{
						break;
					}
				}
				
				fButton.defaultLabelProperties.textFormat = 
					textFormatDefault;
				fButton.hoverLabelProperties.textFormat = 
					textFormatDefault;
				fButton.upLabelProperties.textFormat = 
					textFormatDefault;
				fButton.downLabelProperties.textFormat = 
					textFormatDefault;
				fButton.disabledLabelProperties.textFormat = 
					textFormatDefault;
				fButton.defaultLabelProperties.alpha = 1;
				fButton.hoverLabelProperties.alpha = 1;
				fButton.upLabelProperties.alpha = 1;
				fButton.downLabelProperties.alpha = 1;
				fButton.disabledLabelProperties.alpha = 0.4;
				fButton.defaultLabelProperties.isHTML = true;
				fButton.hoverLabelProperties.isHTML = true;
				fButton.upLabelProperties.isHTML = true;
				fButton.downLabelProperties.isHTML = true;
				fButton.disabledLabelProperties.isHTML = true;
			}
			
			return fButton;
		}
		
		public static function generateRoundRectRadio( 
			width:Number, height:Number, 
			ratio:Number, raduis:Number,
			textSize:String
		):Radio
		{
			var fButton:Radio = new Radio();
			fButton.width = width * ratio;
			fButton.height = height * ratio;
			fButton.defaultSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "default"
			);
			fButton.downSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "down"
			);
			fButton.disabledSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "disabled"
			);
			fButton.defaultSelectedSkin = drawRoundRectImage2(
				height, ColorLib.ORANGE, ratio, raduis, "default"
			);
			
			var fontNames:String = "微软雅黑,宋体";
			var textFormatDefault:TextFormat;
			
			switch( textSize )
			{
				case "small":
				{
					textFormatDefault = new TextFormat( fontNames, 24 * ratio, Color.WHITE, true );
					break;
				}
					
				case "large":
				{
					textFormatDefault = new TextFormat( fontNames, 36 * ratio, Color.WHITE, true );
					break;
				}
					
				default:
				{
					break;
				}
			}
			
			fButton.defaultLabelProperties.textFormat = 
				textFormatDefault;
			fButton.disabledLabelProperties.textFormat = 
				textFormatDefault;
			fButton.disabledLabelProperties.alpha = 0.4;
			return fButton;
		}
		
		public static function generateRoundRectItem( 
			width:Number, height:Number, 
			ratio:Number, raduis:Number,
			textSize:String
		):DefaultListItemRenderer
		{
			var fButton:DefaultListItemRenderer = new DefaultListItemRenderer();
			
			fButton.width = width  * ratio;
			fButton.height = height * ratio;
			fButton.defaultSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "default"
			);
			fButton.downSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "down"
			);
			fButton.defaultSelectedSkin = drawRoundRectImage2(
				height, ColorLib.ORANGE, ratio, raduis, "default"
			);
			
			var fontNames:String = "微软雅黑,宋体";
			var textFormatDefault:TextFormat;
			
			switch( textSize )
			{
				case "small":
				{
					textFormatDefault = new TextFormat( fontNames, 20 * ratio, Color.WHITE, false, 
						null, null, null, null, "center", null, null, null, -5 * ( 1.5 - ratio ) );
					break;
				}
					
				case "large":
				{
					textFormatDefault = new TextFormat( fontNames, 24 * ratio, Color.WHITE, false, 
						null, null, null, null, "center", null, null, null, -5 * ( 1.5 - ratio ) );
					break;
				}
					
				default:
				{
					break;
				}
			}
			
			fButton.defaultLabelProperties.textFormat = 
				textFormatDefault;
			return fButton;
		}
		
		public static function generateRoundRectTextInput( 
			width:Number, height:Number, 
			ratio:Number, raduis:Number
		):TextInput
		{
			var fTextInput:TextInput = new TextInput();
			
			fTextInput.width = width  * ratio;
			fTextInput.height = height * ratio;
			fTextInput.backgroundSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "default"
			);
			fTextInput.backgroundDisabledSkin = drawRoundRectImage2(
				height, Color.WHITE, ratio, raduis, "disabled"
			);
			fTextInput.backgroundFocusedSkin = drawRoundRectImage2(
				height, ColorLib.ORANGE, ratio, raduis, "default"
			);
			
			var fontNames:String = "微软雅黑,宋体";
			var textFormatDefault:TextFormat;
			
			fTextInput.padding = 5 * ratio;
			fTextInput.maxChars = 100;
			fTextInput.textEditorProperties.fontFamily = fontNames;
			fTextInput.textEditorProperties.fontSize = 24 * ratio;
			fTextInput.textEditorProperties.color = Color.WHITE;
			return fTextInput;
		}
		
		/**
		 * 通过矢量绘图构建圆角矩形的FImage 
		 * @param width 宽度
		 * @param height 高度
		 * @param color 填充边线颜色
		 * @param ratio 缩放比
		 * @param raduis 圆角半径
		 * @param states 用于按钮三态
		 * @return  FImage
		 * 
		 */		
		public static function drawRoundRectImage2( 
			height:Number, color:uint, 
			ratio:Number, raduis:Number, states:String
		):Scale3Image
		{
			var image:Scale3Image;
			var rHeight:Number = height * ratio;
			var cornerRaduis:Number = raduis * ratio;
			var shape:Shape = new Shape();
			var matrix:Matrix = new Matrix();
			var bd:BitmapData = new BitmapData( cornerRaduis * 2 + 3, rHeight + 2, true, 0xffffff ); 
			var texture:Scale3Textures;
			var mAngle:Number = Math.PI * 0.5;
			var deltaAlpha:Number = 0;
			
			switch(states)
			{
				case "down":
				{
					mAngle = Math.PI * 1.5;
					break;
				}
					
				case "disabled":
				{
					deltaAlpha = 0.2;
					break;
				}
					
				default:
				{
					break;
				}
			}
			
			matrix.createGradientBox( cornerRaduis * 2 + 1, rHeight, mAngle, 0, 0 );
			shape.graphics.lineStyle( 0, color, 1, true, LineScaleMode.NONE, 
				CapsStyle.SQUARE, JointStyle.ROUND, 255 );
			shape.graphics.lineGradientStyle( "linear", [ color, color ], [ 0.4 - deltaAlpha, 0.8 - deltaAlpha ], [ 10, 200 ], matrix, SpreadMethod.PAD, InterpolationMethod.LINEAR_RGB);
			shape.graphics.beginGradientFill( "linear", [ color, color ], [ 0.2 - deltaAlpha, 0 ], [ 0, 150 ], matrix, SpreadMethod.PAD, InterpolationMethod.LINEAR_RGB);
			shape.graphics.drawRoundRect( 0, 0, cornerRaduis * 2 + 1, rHeight, cornerRaduis, cornerRaduis);
			shape.graphics.endFill(); 
			bd.draw( shape );
			
			var te:Texture = Texture.fromBitmapData( bd, false );
		
			texture = new Scale3Textures( te, cornerRaduis, 1 );
			image = new Scale3Image( texture );
			shape = null;
			bd.dispose();
			bd = null;
			matrix = null;
			return image;
		}
		
		
		/**
		 * 设置没有按钮背景的按钮 
		 * @param w 按钮宽
		 * @param h 按钮高
		 * @param ratio 缩放比
		 * @return 按钮
		 * 
		 */		
		public static function getNoBGStyleBtn( w:Number, h:Number, ratio:Number ):Button
		{
			var button:Button;
			
			button = new Button();
			
			button.width = w  * ratio;
			button.height = h * ratio;
			
			var fontNames:String = "微软雅黑,宋体";
			var textFormatDefault:TextFormat = new TextFormat( fontNames, 24 * ratio, Color.WHITE, true );
			
			button.defaultLabelProperties.textFormat = 
				textFormatDefault;
			button.hoverLabelProperties.textFormat = 
				textFormatDefault;
			button.upLabelProperties.textFormat = 
				textFormatDefault;
			button.downLabelProperties.textFormat = 
				textFormatDefault;
			button.disabledLabelProperties.textFormat = 
				textFormatDefault;
			button.defaultLabelProperties.alpha = 1;
			button.hoverLabelProperties.alpha = 1;
			button.upLabelProperties.alpha = 1;
			button.downLabelProperties.alpha = 1;
			button.disabledLabelProperties.alpha = 0.4;
			return button;
		}
		
		/**
		 * 深度析构Feather 按钮 
		 * @param button 按钮
		 * 
		 */		
		public static function deepDisposeButtonComp( button:Button ):void
		{
			if ( button.defaultSkin )
			{
				deepDisposeScale3Image( button.defaultSkin as Scale3Image );
				button.defaultSkin = null;
			}
			if ( button.defaultSelectedSkin )
			{
				deepDisposeScale3Image( button.defaultSelectedSkin as Scale3Image );
				button.defaultSelectedSkin = null;
			}
			if ( button.downSkin )
			{
				deepDisposeScale3Image( button.downSkin as Scale3Image );
				button.downSkin = null;
			}
			if ( button.disabledSkin )
			{
				deepDisposeScale3Image( button.disabledSkin as Scale3Image );
				button.disabledSkin = null;
			}
			if ( button.defaultIcon )
			{
				( button.defaultIcon as Image ).texture.dispose();
				button.defaultIcon.dispose();
				button.defaultIcon = null;
			}
			if ( button.defaultSelectedIcon )
			{
				( button.defaultSelectedIcon as Image ).texture.dispose();
				button.defaultSelectedIcon.dispose();
				button.defaultSelectedIcon = null;
			}
		}
		
		/**
		 * 深度析构Scale3Image
		 * @param Scale3Image
		 * 
		 */		
		public static function deepDisposeScale3Image( image:Scale3Image ):void
		{
			image.textures.first.dispose();
			image.textures.second.dispose();
			image.textures.third.dispose();
			image.textures.texture.dispose();
			image.dispose();
		}
		
		/**
		 * 深度析构Feather 输入框 
		 * @param textInput 输入框
		 * 
		 */		
		public static function deepDisposeTextInputComp( textInput:TextInput ):void
		{
			if ( textInput.backgroundSkin )
				deepDisposeScale3Image( textInput.backgroundSkin as Scale3Image );
			if ( textInput.backgroundDisabledSkin )
				deepDisposeScale3Image( textInput.backgroundDisabledSkin as Scale3Image );
			if ( textInput.backgroundFocusedSkin )
				deepDisposeScale3Image( textInput.backgroundFocusedSkin as Scale3Image );
		}
		
		//-----------------------------
		// events
		//-----------------------------
		
		//-----------------------------
		// dispose
		//-----------------------------
		
		//-----------------------------
		// construction
		//-----------------------------
		
		/**
		 * 构造函数
		 *
		 */
		public function FG2FUGenerator()
		{
		}
	}
}